package com.jk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.jk.model.ProductBean;
import com.jk.model.ShopBean;
import com.jk.model.UserBean;
import com.jk.service.SLFService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("slf")
public class SLFController {

    @Reference(check = false)
    private SLFService slfService;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 登录
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String login(String callback,UserBean userBean, HttpServletRequest request){
        HashMap<String, String> result = new HashMap<>();
        UserBean userInfo = slfService.findUserInfoByAccount(userBean.getUserAccount());
        if (userInfo == null) {
            //账号密码错误
            result.put("code", "1");
        }
        if (!userInfo.getUserPassword().equals(userBean.getUserPassword())) {
            //账号密码错误
            result.put("code", "2");
        }

        String stringId = userInfo.getId().toString();
        result.put("stringId",stringId);

        HttpSession session = request.getSession();
        session.setAttribute(session.getId(),userInfo);
        //登录成功
        result.put("code", "0");
        //保存jwt认证信息
        String res = JSON.toJSONString(result);
        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去了
        res = callback + "(" + res + ")";
        return res;
    }


    /**
     * 查询热门推荐商品
     * @param callback
     * @return
     */
    @RequestMapping("findProductState")
    @ResponseBody
    public String findProductState(String callback){
        List<Map> productBeans = slfService.findProductState();
        String res = JSON.toJSONString(productBeans);
        res = callback + "(" + res + ")";
        return res;
    }

    /**
     * 查询最新商品
     * @param callback
     * @return
     */
    @RequestMapping("findTheLatest")
    @ResponseBody
    public String findTheLatest(String callback){
        List<Map> productBeans = slfService.findTheLatest();
        String res = JSON.toJSONString(productBeans);
        res = callback + "(" + res + ")";
        return res;
    }


    /**
     * 根据Id查询商品
     * @param callback
     * @param id
     * @return
     */
    @RequestMapping("findParticularsApp")
    @ResponseBody
    public String findParticularsApp(String callback,Integer id){
        List<Map> infoById = slfService.findParticularsApp(id);
        String res = JSON.toJSONString(infoById);
        res = callback + "(" + res + ")";
        return res;
    }

    /**
     * 查询所有商品
     * @param callback
     * @return
     */
    @RequestMapping("findClassify")
    @ResponseBody
    public String findClassify(String callback,Integer flag){
        List<Map> productBeans = slfService.findClassify(flag);
        String res = JSON.toJSONString(productBeans);
        res = callback + "(" + res + ")";
        return res;
    }

    /**
     * 查询登录的个人信息
     * @param callback
     * @param
     * @return
     */
    @RequestMapping("findPersonalCenter")
    @ResponseBody
    public String findPersonalCenter(String callback,HttpServletRequest request){
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        List<Map> personalCenter = slfService.findPersonalCenter(attribute.getId());
        String res = JSON.toJSONString(personalCenter);
        res = callback + "(" + res + ")";
        return res;
    }


    /**
     * 添加购物车
     * @param callback
     * @param productId
     * @param request
     * @return
     */
    @RequestMapping("saveShopApp")
    @ResponseBody
    public String saveShopApp(String callback,Integer productId,HttpServletRequest request){
        HashMap<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        if (attribute == null){
            result.put("code","1");
            result.put("msg","请登录");
            return null;
        }
        ShopBean shopBean = new ShopBean();
        shopBean.setCreateTime(new Date().toString());
        shopBean.setProductId(productId);
        shopBean.setUserId(attribute.getId());
        result.put("code","0");
        result.put("msg","加入成功");
        mongoTemplate.save(shopBean);
        String res = JSON.toJSONString(result);
        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去了
        res = callback + "(" + res + ")";
        return res;
    }


    /**
     * 查询购物车
     * @param callback
     * @return
     */
    @RequestMapping("findShopApp")
    @ResponseBody
    public String findShopApp(String callback,Integer userId){
        HashMap<String, Object> result = new HashMap<>();
        //查询mongoDB
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        List<ShopBean> shopBeans = mongoTemplate.find(query, ShopBean.class);

        ArrayList<ProductBean> list = new ArrayList<>();
        for (ShopBean shopBean : shopBeans) {
            ProductBean shopApp = slfService.findShopApp(shopBean.getProductId());
            list.add(shopApp);
        }
        result.put("rows",list);
        String res = JSON.toJSONString(result);
        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去了
        res = callback + "(" + res + ")";
        return res;
    }



    //注册
    @RequestMapping("saveFeg")
    @ResponseBody
    public String saveFeg(String callback,UserBean userBean){
        HashMap<String, Object> result = new HashMap<>();
        UserBean queryUser = slfService.findUserInfoByAccount(userBean.getUserAccount());
        if (queryUser != null) {
            return "用户已存在";
        } else {
            slfService.saveFeg(userBean);
            result.put("code","0");
        }
        String res = JSON.toJSONString(result);
        res = callback + "(" + res + ")";
        return res;
    }


}
